<template>
  <el-upload
    :data="data"
    :id="id"
    :action="action"
    :show-file-list="false"
    :on-success="handleAvatarSuccess"
    :before-upload="beforeAvatarUpload"
  >
    <slot></slot>
  </el-upload>
</template>

<script>
export default {
  props: ['url', 'id', 'data', 'type'],
  data() {
    return {
      action: ''
    }
  },
  mounted() {
    if (this.url) {
      this.action = PATH + this.url
    } else {
      this.action = PATH + '/platform/getPicAddress.do'
    }
  },
  methods: {
    handleAvatarSuccess(res, file) {
      if (res.status == '0') {
        this.$emit('input', res.data)
        _g.message('上传成功', 'success')
      } else {
        _g.message('上传失败', 'warning')
      }
    },
    beforeAvatarUpload(file) {
      console.log(file)
      if (this.type === '0') {
        if (
          ['image/jpg', 'image/png', 'image/jpeg', 'image/gif'].indexOf(
            file.type
          ) == -1
        ) {
          _g.message('请上传图片', 'warning')
          return false
        }
        return true
      } else {
        var testmsg = file.name.substring(file.name.lastIndexOf('.') + 1)
        const extension = testmsg === 'docx'
        const extension1 = testmsg === 'doc'
        const extension2 = testmsg === 'pdf'

        if (!extension && !extension1 && !extension2) {
          _g.message('请上传正确格式的文件', 'warning')
          return false
        }
        return true
      }
    }
  },
  components: {}
}
</script>

<style lang="scss">
.el-upload {
  display: inline-block;
}
</style>
